﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XinYi.CrookedShow.Core;
using XinYi.CrookedShowMode;

namespace XinYi.CroodedShowFront.DAL
{
    public class PageRepository
    {
        /// <summary>
        /// 查询商品对象
        /// </summary>
        /// <param name="productName"></param>
        /// <returns></returns>
        #region 查询商品对象
        public Product_Spu product(int Id)
        {
            string sql = "select * from Product_Spu where Id=" + Id;
            return DapperHelper<Product_Spu>.QuerySingleOrDefault(sql, null);
        }
        #endregion
        /// <summary>
        /// 查询商品关联的规格值
        /// </summary>
        /// <param name="productName"></param>
        /// <returns></returns>
        #region 查询商品关联的规格值
        public static List<Produc_Spu_Spec> Produc_Spu_Spec(int Id)
        {
            string sql = "select * from Produc_Spu_Spec where Produc_Spu_Id=" + Id;
            return DapperHelper<Produc_Spu_Spec>.QueryList(sql, null);
        }
        #endregion
        /// <summary>
        /// 查询规格
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        #region 查询规格
        public static List<Spec> Spec(int Id)
        {
            List<Produc_Spu_Spec> Spu_Spec = Produc_Spu_Spec(Id);
            string Ids = "";

            for (int i = 0; i < Spu_Spec.Count; i++)
            {
                if (i != Spu_Spec.Count - 1)
                {

                    Ids += Spu_Spec[i].Spec_Id + ",";
                }
                else
                {
                    Ids += Spu_Spec[i].Spec_Id;
                }
            }


            string sql = "select * from Spec where Id in(" + Ids + ")";
            return DapperHelper<Spec>.QueryList(sql, null);
        }
        #endregion
        /// <summary>
        /// 查看规格值
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        #region 查看规格值
        public List<Spec_Value> Spec_Value(int Id)
        {
            List<Spec> Specs = Spec(Id);
            string Ids = "";

            for (int i = 0; i < Specs.Count; i++)
            {
                if (i != Specs.Count - 1)
                {

                    Ids += Specs[i].Id + ",";
                }
                else
                {
                    Ids += Specs[i].Id;
                }
            }
            string sql = "select * from Spec_Value where Spec_Id in(" + Ids + ")";
            return DapperHelper<Spec_Value>.QueryList(sql, null);
        }
        #endregion
        /// <summary>
        /// 查询商品 Product_Sku表库存
        /// </summary>
        /// <param name="productName"></param>
        /// <returns></returns>
        #region Product_Sku表库存
        public int Product_Sku(int Id)
        {
            string sql = "select Sum(Stock) from Product_Sku where ProductId=" + Id;
            return Convert.ToInt32(DapperHelper<Product_Sku>.QueryScalar(sql, null));
        }
        #endregion
        /// <summary>
        /// 查询商品评论
        /// </summary>
        /// <param name="productName"></param>
        /// <returns></returns>
        #region 查询商品评论
        public List<Product_Comment> Product_Comment(int Id)
        {
            string sql = "select * from Product_Comment where Product_Id=" + Id;
            return DapperHelper<Product_Comment>.QueryList(sql, null);
        }
        #endregion
        #region 查询商品图片
        public List<Pic> Pics(int Id)
        {
            string sql = "select * from Pic where ProductId=" + Id;
            return DapperHelper<Pic>.QueryList(sql, null);
        }
        #endregion
        #region 分类
        public List<Category> Categorys(Product_Spu product_Spu)
        {
            string sql = "select * from Category where Id in ("+ product_Spu .One_Category_Id+ ","+ product_Spu .Three_Category_Id+ ","+ product_Spu .Two_Category_Id+ ")";
            return DapperHelper<Category>.QueryList(sql, null);
        }
        #endregion
    }
}
